package com.heifeng.cloudshelf.utils;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.ansen.http.net.HTTPCaller;
import com.ansen.http.net.NameValuePair;
import com.ansen.http.net.RequestDataCallback;
import com.heifeng.cloudshelf.base.BaseResponse;
import com.heifeng.cloudshelf.dao.UploadBean;
import com.heifeng.cloudshelf.helper.MyObserver;
import com.heifeng.cloudshelf.helper.RxUtil;
import com.heifeng.cloudshelf.network.NetClient;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import io.reactivex.disposables.Disposable;

public class UpdateUtils {

    private OnHttpResultCallBack onHttpResultCallBack;
    private Context _context;
    public static String downLoadUrl = "";

    public UpdateUtils(OnHttpResultCallBack onHttpResultCallBack, Context _context) {
        this.onHttpResultCallBack = onHttpResultCallBack;
        this._context = _context;
    }

    public void startCheckVersion(String apiKey) {
        Map<String, String> map = new HashMap<>();
        //http://api.fir.im/apps/latest/5deb110623389f5d421ebca1?api_token=b4f10dfef10d3c5659c90a221baf8e00&type=android
        String url = "http://api.fir.im/apps/latest/5deb110623389f5d421ebca1?api_token=" + apiKey + "&type=android";
//        map.put("api_token", apiKey);
//        map.put("type", "android");
        httpPostUrl(url, map);
    }

    public void net() {
        NetClient.getInstance().getNetApi().getVersionUpdateInfo()
                .compose(RxUtil.rxSchedulers())
                .subscribe(new MyObserver<BaseResponse>(_context) {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @Override
                    public void onNext(BaseResponse baseResponse) {

                    }
                });
    }

    private void httpPostUrl(String url, Map<String, String> map) {
        List<NameValuePair> postParam = new ArrayList<>();
        if (null != map && map.size() > 0) {
            Iterator<String> iterator = map.keySet().iterator();
            while (iterator.hasNext()) {
                String next = iterator.next();
                postParam.add(new NameValuePair(next, map.get(next)));
            }
        }
        HTTPCaller.getInstance().get(UploadBean.class, url, null, new RequestDataCallback<UploadBean>() {
            @Override
            public void dataCallback(UploadBean obj) {
                if (null != obj) {
                    Log.e("HTTPUPDATE", obj.toString());
                    checkVersion(obj);
                }
            }
        });
//        HTTPCaller.getInstance().get(UploadBean.class, url, null, postParam, new RequestDataCallback<UploadBean>() {
//            @Override
//            public void dataCallback(UploadBean obj) {
//                if (null != obj) {
//                    Log.e("HTTPUPDATE", obj.toString());
//                    checkVersion(obj);
//                }
//            }
//        });
    }

    private void checkVersion(UploadBean upload) {
        int versionCode = PackageUtils.getVersionCode(_context);
        String versionName = PackageUtils.getVersionName(_context);
        if (versionCode < Integer.parseInt(upload.version)) {
            //TODO 更新
            if (null != onHttpResultCallBack) {
                LogUtils.e("upload.install_url:"+upload.install_url);
                downLoadUrl = upload.install_url.substring(0,upload.install_url.indexOf("&"));
                onHttpResultCallBack.onResult(true, upload.versionShort);
            }
        } else {
            if (null != onHttpResultCallBack) {
                onHttpResultCallBack.onResult(false, versionName);
            }
        }
    }

    public interface OnHttpResultCallBack {
        void onResult(boolean isNewVersion, String version);
    }
}
